forv y = 2014/2018 {
	local dir_list : dir "${raw_data}/physician_compare/doctors_and_clinicians_archive_`y'" dirs "*"
	
	foreach x of local dir_list {
		
		local csv_file: dir "${raw_data}/physician_compare/doctors_and_clinicians_archive_`y'/`x'" files `"*.csv"'

		import delimited `"${raw_data}/physician_compare/doctors_and_clinicians_archive_`y'/`x'/`:word 1 of `csv_file''"', varnames(nonames) clear
						
		cap rename (v1 v9 v10 v11) (npi cred med_sch grd_yr)
		
		keep npi cred med_sch grd_yr
		
		duplicates drop
		
		gen year = "`y'"
		
		tostring npi, replace
		tostring grd_yr, replace
		
		replace npi = ""		if inlist(npi, " ", ".")
		replace grd_yr = ""		if inlist(grd_yr, " ", ".")
		replace cred = ""		if inlist(cred, " ", ".")
		
		drop if inlist(npi, "NPI", "")
		drop if mi(grd_yr)
		drop if inlist(med_sch, "OTHER")
				
		compress
						
		save "${intermediate_data}/physician_compare/intermediate/`x'.dta", replace
	}
}

clear

forv y = 2014/2018 {
	local dir_list : dir "${raw_data}/physician_compare/doctors_and_clinicians_archive_`y'" dirs "*"
	
	foreach x of local dir_list {
		
		append using "${intermediate_data}/physician_compare/intermediate/`x'.dta"
		
		sort npi year
				
	}
}

collapse (last) cred med_sch grd_yr, by(npi)

destring npi, replace
destring grd_yr, replace

gisid npi

save "${intermediate_data}/physician_compare/physician_compare_cleaned.dta", replace
